package com.woniuxy.learnmap.service.impl;

import com.netflix.discovery.converters.Auto;
import com.woniuxy.commons.dto.RouteDetailDto;
import com.woniuxy.commons.dto.RouteDto;
import com.woniuxy.commons.dto.RouteSearchDto;
import com.woniuxy.commons.entity.*;
import com.woniuxy.commons.exception.EtmsServiceException;
import com.woniuxy.commons.exception.learnmap.RouteDetailALReadyExistException;
import com.woniuxy.commons.exception.learnmap.RouteNameIsAlReadyExistException;
import com.woniuxy.commons.service.RemoteCourseService;
import com.woniuxy.commons.service.RemoteExamService;
import com.woniuxy.learnmap.mapper.RouteDetailMapper;
import com.woniuxy.learnmap.mapper.RouteDetailRecordMapper;
import com.woniuxy.learnmap.mapper.RouteMapper;
import com.woniuxy.learnmap.service.RouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: 王敞
 * @E-mail： 365799712@qq.com
 * @Date: 2020/07/17/12:16
 * @Description: 路径service实现类
 */
@Service
public class RouteServiceImpl implements RouteService {
    @Autowired
    private RouteDetailMapper routeDetailMapper;
    @Autowired
    private RouteMapper routeMapper;
    @Autowired
    private RouteDetailRecordMapper routeDetailRecordMapper;
    @Autowired
    private RemoteExamService remoteExamService;
    @Autowired
    private RemoteCourseService remoteCourseService;

    @Override
    public void addRoute(Route route, List<RouteDetailDto> routeDetails) {
        try {
            //先判断路径名字是否已存在，存在报自定义错误
            Route route1 = routeMapper.findRouteByName(route.getName());
            if (route1 != null) {
                throw new RouteNameIsAlReadyExistException("路径名字已存在");
            }
            //再根据路径详情拼接路径记录名，判断是否已存在，先拼接
            String recordName = "";
            for (RouteDetailDto routeDetail : routeDetails) {
                recordName += routeDetail.getOrderno() + routeDetail.getStyle() + routeDetail.getRid();
            }
            //再通过拼接的recordName去查询
            LearnMapRouteDetailRecord record = routeDetailRecordMapper.findLMDRecord(recordName);
            if (record != null) {
                throw new RouteDetailALReadyExistException("已存在这种构成的路径详情");
            }
            //添加路径，
            System.out.println(route);
            routeMapper.addRoute(route);
            //通过名字找寻刚添加的这条路径，获取路径id
            route1 = routeMapper.findRouteByName(route.getName());
            int id = route1.getRouteid();
            //给路详情赋值路径id,并添加到路径详情表中
            for (RouteDetailDto routeDetail : routeDetails) {
                routeDetail.setRouteid(id);
                routeDetailMapper.addRouteDetail(routeDetail);
            }
            //添加路径详情记录
            record = new LearnMapRouteDetailRecord();
            record.setRecordName(recordName).setStatus("1");
            routeDetailRecordMapper.addRouteDetailRecord(record);
        } catch (Exception e) {
            throw new EtmsServiceException("系统维护中", e);
        }
    }

    @Override
    public PageBean<RouteDto> findRoutesByPage(RouteSearchDto routeSearchDto) {
        try {
            int index = (routeSearchDto.getPage() - 1) * routeSearchDto.getPageSize();
            routeSearchDto.setIndex(index);
            List<RouteDto> data = routeMapper.findRoutesByPage(routeSearchDto);
            int totalNums = routeMapper.findTotalNumsOfRoutes(routeSearchDto);
            int totalPage = totalNums % routeSearchDto.getPageSize() == 0 ? totalNums / routeSearchDto.getPageSize() :
                    totalNums / routeSearchDto.getPageSize() + 1;
            PageBean<RouteDto> pb = new PageBean<>();
            pb.setTotalPage(totalPage).setTotalNum(totalNums).setPageSize(routeSearchDto.getPageSize())
                    .setPage(routeSearchDto.getPage()).setNumsOfCurrPage(data.size()).setData(data);
            return pb;
        } catch (Exception e) {
            e.printStackTrace();
            throw new EtmsServiceException("系统维护中");
        }

    }

    @Override
    public void deleteRouteById(int routeId) {
        try {
            routeMapper.deleteRouteById(routeId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new EtmsServiceException("系统维护中");
        }
    }
    /*
    *
     * @Description: 查询路径详情相关的考试或者课程
     * @Param: [routeid]
     * @return: java.util.List<com.woniuxy.commons.dto.RouteDetailDto>
     * @Author: 王敞
     * @Date: 21:24 2020/8/1
     **/
    @Override
    public List<RouteDetailDto> findRouteDetails(int routeid) {
        try {
            List<RouteDetailDto> detailDtos = routeDetailMapper.findRouteDetails(routeid);
            //根据类型的不同，查询考试或者课程
            for (RouteDetailDto routeDetailDto : detailDtos) {
                if (routeDetailDto.getStyle().equals("1")) {
                    ResponseEntity<Course> data = remoteCourseService.findCourseByCid(routeDetailDto.getRid());
                    routeDetailDto.setName(data.getData().getCname());
                    routeDetailDto.setStyleName("课程");
                } else {
                    Exam exam=new Exam();
                    exam.setId(routeDetailDto.getRid());
                    exam=remoteExamService.findExamById(exam);
                    routeDetailDto.setName(exam.getName());
                    routeDetailDto.setStyleName("考试");
                }
            }
            return detailDtos;
        } catch (Exception e) {
            throw new EtmsServiceException("系统维护中");
        }
    }
}
